package coop.bancocredicoop.proyectos.gd.controller.jsonTiposDocumento;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import coop.bancocredicoop.proyectos.gd.domain.DocumentType;
import coop.bancocredicoop.proyectos.gd.query.documentType.
		FindAllDocumentTypeOrderByName;

public class JsonTiposDocumentoController {

	public void run(HttpServletRequest request) throws Exception {

		StringBuilder strbld = new StringBuilder("{\"documentTypes\":{");

		List<DocumentType> listDocumentType =
				new FindAllDocumentTypeOrderByName().run();

		int i = 0;

		for (DocumentType documentType : listDocumentType) {
			if (i++ > 0)
				strbld.append(",");
			strbld.append("\"");
			strbld.append(documentType.getId());
			strbld.append("\":{");
			strbld.append("\"purgeable\":");
			strbld.append(documentType.getPurgeable());
			strbld.append(",\"hasExpiration\":");
			strbld.append(documentType.getHasExpiration());
			strbld.append(",\"defaultExpirationDay\":\"");
			strbld.append(new SimpleDateFormat(request.
					getParameter("dateFormat")).
					format(documentType.getDefaultExpirationDay()));
			strbld.append("\"}");
		}

		strbld.append("}}");

		request.setAttribute("jsonDocumentType", strbld.toString());

	}

}
